package com.cst.test;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 定时任务，主类加上@Component和@EnableScheduling注解，在方法上加上@Scheduled注解
 * 注意的是，可以加个开关来决定任务是否执行
 */
@Slf4j
@Component
@EnableScheduling
public class Method3 {
    //从配置文件中读取开关的值
    private boolean flag = false;

    @Scheduled(cron = "0 48 21 * * ?")
    public void m1() {
//判断开关状态
        if (flag) {
            SimpleDateFormat stf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = stf.format(new Date());
            System.out.println("定时任务启动->>" + format);
        } else {
            System.out.println("未开启定时任务开关，请重试！");
        }
    }
}